# Python 运算符示例

print("=" * 50)
print("Python 运算符")
print("""
1. 数字类型 (Number)：包括整数、浮点数、复数和布尔值
2. 字符串类型 (String)：单引号、双引号、三引号字符串及常用操作
3. 列表类型 (List)：可变序列类型及常用操作
4. 元组类型 (Tuple)：不可变序列类型及特点
5. 集合类型 (Set)：无序不重复元素集及集合运算
6. 字典类型 (Dictionary)：键值对数据结构及操作
7. 数据类型分类：可变与不可变类型的区别
8. 类型检查：如何检查变量的数据类型
9. 类型转换：不同类型之间的转换示例
""")
print("=" * 50)

# 1. 算术运算符
print("\n1. 算术运算符")
print("-" * 30)

a, b = 10, 3
print(f"a = {a}, b = {b}")
print(f"a + b = {a + b}")    # 加法
print(f"a - b = {a - b}")    # 减法
print(f"a * b = {a * b}")    # 乘法
print(f"a / b = {a / b}")    # 除法
print(f"a // b = {a // b}")  # 取整除法
print(f"a % b = {a % b}")    # 取模（求余数）
print(f"a ** b = {a ** b}")  # 幂运算

# 2. 比较运算符
print("\n\n2. 比较运算符")
print("-" * 30)

x, y = 5, 10
print(f"x = {x}, y = {y}")
print(f"x == y: {x == y}")   # 等于
print(f"x != y: {x != y}")   # 不等于
print(f"x > y: {x > y}")     # 大于
print(f"x < y: {x < y}")     # 小于
print(f"x >= y: {x >= y}")   # 大于等于
print(f"x <= y: {x <= y}")   # 小于等于

# 字符串比较
str1, str2 = "apple", "banana"
print(f"\nstr1 = '{str1}', str2 = '{str2}'")
print(f"str1 == str2: {str1 == str2}")
print(f"str1 < str2: {str1 < str2}")  # 按字典序比较

# 3. 赋值运算符
print("\n\n3. 赋值运算符")
print("-" * 30)

z = 20
print(f"z = {z}")
z += 5   # 等价于 z = z + 5
print(f"z += 5  => z = {z}")
z -= 3   # 等价于 z = z - 3
print(f"z -= 3  => z = {z}")
z *= 2   # 等价于 z = z * 2
print(f"z *= 2  => z = {z}")
z /= 4   # 等价于 z = z / 4
print(f"z /= 4  => z = {z}")
z %= 3   # 等价于 z = z % 3
print(f"z %= 3  => z = {z}")
z **= 2  # 等价于 z = z ** 2
print(f"z **= 2 => z = {z}")
z //= 2  # 等价于 z = z // 2
print(f"z //= 2 => z = {z}")

# 4. 逻辑运算符
print("\n\n4. 逻辑运算符")
print("-" * 30)

p, q = True, False
print(f"p = {p}, q = {q}")
print(f"p and q: {p and q}")  # 与运算
print(f"p or q: {p or q}")    # 或运算
print(f"not p: {not p}")      # 非运算

# 逻辑运算符的短路特性
print("\n逻辑运算符短路特性:")
def true_func():
    print("执行了 true_func()")
    return True

def false_func():
    print("执行了 false_func()")
    return False

print("False and true_func():")
result1 = False and true_func()  # true_func() 不会被执行
print(f"结果: {result1}")

print("\nTrue or false_func():")
result2 = True or false_func()   # false_func() 不会被执行
print(f"结果: {result2}")

# 5. 位运算符
print("\n\n5. 位运算符")
print("-" * 30)

m, n = 60, 13  # 60 的二进制是 0011 1100, 13 的二进制是 0000 1101
print(f"m = {m} (二进制: {bin(m)})")
print(f"n = {n} (二进制: {bin(n)})")
print(f"m & n = {m & n} (二进制: {bin(m & n)})")   # 按位与
print(f"m | n = {m | n} (二进制: {bin(m | n)})")   # 按位或
print(f"m ^ n = {m ^ n} (二进制: {bin(m ^ n)})")   # 按位异或
print(f"~m = {~m} (二进制: {bin(~m & 0xFF)})")     # 按位取反
print(f"m << 2 = {m << 2} (二进制: {bin(m << 2)})") # 左移
print(f"m >> 2 = {m >> 2} (二进制: {bin(m >> 2)})") # 右移

# 6. 成员运算符
print("\n\n6. 成员运算符")
print("-" * 30)

# in 运算符
fruits = ["apple", "banana", "cherry"]
print(f"fruits = {fruits}")
print(f"'banana' in fruits: {'banana' in fruits}")
print(f"'orange' in fruits: {'orange' in fruits}")

# not in 运算符
print(f"'grape' not in fruits: {'grape' not in fruits}")

# 字符串中的成员运算
text = "Hello, Python!"
print(f"\ntext = '{text}'")
print(f"'Python' in text: {'Python' in text}")
print(f"'Java' not in text: {'Java' not in text}")

# 7. 身份运算符
print("\n\n7. 身份运算符")
print("-" * 30)

list1 = [1, 2, 3]
list2 = [1, 2, 3]
list3 = list1

print(f"list1 = {list1}")
print(f"list2 = {list2}")
print(f"list3 = list1")

print(f"list1 == list2: {list1 == list2}")  # 值相等
print(f"list1 is list2: {list1 is list2}")  # 对象相同
print(f"list1 is list3: {list1 is list3}")  # 对象相同

print(f"list1 的 id: {id(list1)}")
print(f"list2 的 id: {id(list2)}")
print(f"list3 的 id: {id(list3)}")

# 8. 运算符优先级
print("\n\n8. 运算符优先级")
print("-" * 30)

# 括号可以改变运算优先级
result1 = 2 + 3 * 4
result2 = (2 + 3) * 4
print(f"2 + 3 * 4 = {result1}")
print(f"(2 + 3) * 4 = {result2}")

# 复杂表达式
x, y, z = 10, 5, 2
result3 = x + y * z ** 2 - 1  # 等价于 10 + 5 * (2 ** 2) - 1
print(f"x + y * z ** 2 - 1 (x={x}, y={y}, z={z}): {result3}")

# 9. 三元运算符（条件表达式）
print("\n\n9. 三元运算符")
print("-" * 30)

age = 18
status = "成年人" if age >= 18 else "未成年人"
print(f"age = {age}, status = {status}")

score = 85
grade = "优秀" if score >= 90 else ("良好" if score >= 80 else "一般")
print(f"score = {score}, grade = {grade}")

# 10. 运算符重载示例
print("\n\n10. 运算符重载示例")
print("-" * 30)

class Vector:
    def __init__(self, x, y):
        self.x = x
        self.y = y

    def __add__(self, other):
        return Vector(self.x + other.x, self.y + other.y)

    def __str__(self):
        return f"Vector({self.x}, {self.y})"

    def __eq__(self, other):
        return self.x == other.x and self.y == other.y

v1 = Vector(2, 3)
v2 = Vector(1, 4)
v3 = v1 + v2  # 使用重载的 + 运算符

print(f"v1 = {v1}")
print(f"v2 = {v2}")
print(f"v1 + v2 = {v3}")
print(f"v1 == v2: {v1 == v2}")

print("\n" + "=" * 50)
print("运算符示例结束")
print("=" * 50)
